﻿using Aker.DevTeam;
using DevExpress.XtraReports.UI;
using System;
using System.Data;
using System.Drawing;

namespace Aker.Iplik
{
    public partial class SiparisFormu : XtraReport
    {
        string detailTableName;
        int LangId = 0;
        string Currency;

        public SiparisFormu(int POrderId, int langId)
        {
            InitializeComponent();

            ReportPrintTool printTool = new ReportPrintTool(this);

            LangId = langId;

            ds = Data.DM.ExecuteDataSet("GetPOrderReport", POrderId);
            detailTableName = ds.Tables[1].TableName;
            DataRow dr = ds.Tables[0].Rows[0];


            lblTotal.Text = "Toplam: " + dr["Currency"].ToStringNN() + " " + (ds.Tables[2].Rows[0][0].ToDecimalNN()).ToString("n2").Replace(",00", "");


            XRTableRow row = table1.Rows[0];
            row.Cells[0].Text = "Müşteri";
            row.Cells[1].Text = dr["Brand"].ToStringNN();

            row = table1.Rows[1];
            row.Cells[0].Text = "Sipariş No";
            row.Cells[1].Text = "AKR-POrder." + dr["POrderId"].ToStringNN();


            row = table2.Rows[0];
            row.Cells[0].Text = "Teslimat Müşterisi";
            row.Cells[1].Text = dr["TeslimatMusterisi"].ToStringNN();


            row = table2.Rows[1];
            row.Cells[0].Text = "Ödeme Şekli";
            row.Cells[1].Text = dr["PaymentType"].ToStringNN();


            //lblFiyat.Text = lblFiyat.Text.Replace("X", dr["Currency"].ToStringNN());
            //lblBirimAdet.Text = lblBirimAdet.Text.Replace("X", dr["OlcuBirimi"].ToStringNN());

            lblDate.Text = ((DateTime)dr["OrderDate"]).ToString("dd.MM.yyyy");

            tcSiparisiAlan1.Text = dr["SiparisiAlan"].ToStringNN();
            tcSiparisiAlan2.Text = dr["SiparisiAlan"].ToStringNN();

            tcMusteri1.Text = dr["Contact"].ToStringNN();
            tcMusteri2.Text = dr["Contact"].ToStringNN();

            tcNotlar.Text = dr["Notes"].ToStringNN();

            if (langId == 2)
            {
                lblTotal.Text = lblTotal.Text.Replace("Toplam", "Total");
                row = table1.Rows[0];
                row.Cells[0].Text = "Customer";
                row.Cells[1].Text = dr["Brand"].ToStringNN();

                row = table1.Rows[1];
                row.Cells[0].Text = "In Contact";
                row.Cells[1].Text = dr["Contact"].ToStringNN();

                row = table1.InsertRowBelow(table1.Rows[table1.Rows.Count - 1]);
                row.Cells[0].Text = "Order Number";
                row.Cells[1].Text = "AKR-POrder." + dr["POrderId"].ToStringNN();

                row = table1.InsertRowBelow(table1.Rows[table1.Rows.Count - 1]);
                row.Cells[0].Text = "Customer PO#";
                row.Cells[1].Text = dr["CustomerOrderNo"].ToStringNN();


                row = table2.Rows[0];
                row.Cells[0].Text = "Invoice To";
                row.Cells[1].Text = dr["FaturaMusterisi"].ToStringNN();

                row = table2.Rows[1];
                row.Cells[0].Text = "Destination of Goods";
                row.Cells[1].Text = dr["TeslimatMusterisi"].ToStringNN();

                row = table2.InsertRowBelow(table2.Rows[table2.Rows.Count - 1]);
                row.Cells[0].Text = "Delivery Term";
                row.Cells[1].Text = dr["TeslimatSekli"].ToStringNN();

                row = table2.InsertRowBelow(table2.Rows[table2.Rows.Count - 1]);
                row.Cells[0].Text = "Payment Term";
                row.Cells[1].Text = dr["PaymentType"].ToStringNN().Replace("gün", "day(s)").Replace("Nakit", "Cash").Replace("Çek", "Cheque").Replace("Kredi Kartı", "Credit Card").Replace("Not:", "Note:");


                tcSipSart.Text = "1- In order to proceed with production, order confirmation has to be stamped and signed by the buyer and returned to the producer by fax. Fax transmission of the order confirmation is binding for all parties.";
                tcSipSart.Text += "\r\n2- All prices are nett prices anda are exclusive of any duty, value added sales or other types of taxes, fees and bank charges outside Turkey.";
                tcSipSart.Text += "\r\n3- In case of sales made without L/C the buyer is responsible of providing the swift copy of the payment to the supplier on due date. In the event of any payment not being received by the due date shown on the invoice born from this order confirmation, the seller has the right to charge interest from the due date without notice on overdue account as 1% per month of the time being on a daily basis.";
                tcSipSart.Text += "\r\n4- All claims must be raised to the poducer before the fabrics are cut. Any and all claims regarding quality and quantity of the goods, including any claims for damage, shortage and defects above the agreed standards shall be made by buyer within seven days upon delivery of the goods to the buyer. Claims regarding quantity and packaging of the goods shall not be valid unless it is confirmed under the CMR/Bill of Landing of the related delivery. All claims of buyer regarding the quality of goods shoud be accompanied by a sample of the goods showing the defects.";
                tcSipSart.Text += "\r\n5-5- This order confirmation shall be governed and constructed by Turkish Law and Istanbul courts and shall have exclusive jurisdiction in any disputes regarding this order confirmation.";
                tcSipSart.HeightF = 160;

                lblHeader.Text = "ORDER CONFIRMATION";
                lblAciklama.Text = "For any questions please reply to all. Please revert for any discrepancy in 48 hours";


                lblKaliteKodu.Text = "Color";
                lblDenye.Text = "Description";
                lblParlaklik.Text = "Brightness";
                lblOzellik.Text = "Specification";
                lblKompozisyon.Text = "Composition";
                lblBirimAdet.Text = "Unit";
                lblFiyat.Text = lblFiyat.Text.Replace("Fiyat", "Price");
                lblTermin.Text = "Lead Time";

                tcNotlarHeader.Text = "NOTES";
                lblBanka.Visible = false;
                lblBankaHeader.Visible = false;
                xrIng.Visible = true;
                lblIng.Visible = true;
                lblSartHeader.Text = "ORDER POLICY";
                lblTeyit.Text = "IN ORDER TO PROCEED WITH PRODUCTION, ORDER CONFIRMATION HAS TO BE STAMPED AND SIGNED.";

                lblSipAlan1.Text = lblSipAlan2.Text = "SELLER";
                lblMus1.Text = lblMus2.Text = "BUYER";
            }

            ReportHeader.HeightF += 10;

            tblDetail.Rows.Clear();

            for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            {
                DataRow detail = ds.Tables[1].Rows[i];
                XRTableRow xrow = new XRTableRow();

                XRTableCell cell = AddCell(detail, xrow, "IplikRenk", true);
                cell.WidthF = lblKaliteKodu.WidthF;
                cell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;

                AddCell(detail, xrow, "Kalinlik").WidthF = lblDenye.WidthF;
                AddCell(detail, xrow, "Parlaklik").WidthF = lblParlaklik.WidthF;
                AddCell(detail, xrow, "Fantazi").WidthF = lblOzellik.WidthF;
                AddCell(detail, xrow, "Karisim1", false, 10, " İplik").WidthF = lblKompozisyon.WidthF;
                AddCell(detail, xrow, "BirimAdet").WidthF = lblBirimAdet.WidthF;
                AddNumericCell(detail, xrow, "KG").WidthF = lblKG.WidthF;
                AddNumericCell(detail, xrow, "Price", dr["Currency"].ToStringNN() + " ").WidthF = lblFiyat.WidthF;
                cell = AddDateCell(detail, xrow, "ETA");
                cell.WidthF = lblTermin.WidthF;
                cell.Borders = DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Bottom;

                tblDetail.Rows.Add(xrow);

                XRTableRow detailRow = new XRTableRow();

                if (detail["Notes"].ToStringNN() != "")
                {


                    xrow = new XRTableRow();

                    xrow.Cells.Add(OkResmi());

                    XRTableCell trCell = AddCell(detail, xrow, "Notes", true);

                    trCell.Borders = DevExpress.XtraPrinting.BorderSide.Bottom | DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Right;
                    trCell.WidthF = tblDetail.WidthF - lblKaliteKodu.WidthF;
                    trCell.ForeColor = Color.Red;

                    tblDetail.Rows.Add(xrow);
                }
            }
        }

        private XRTableCell OkResmi()
        {
            XRTableCell cell = new XRTableCell();
            cell.WidthF = detailImage.WidthF;
            cell.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Bottom;

            XRPictureBox pictureBox = new XRPictureBox();
            pictureBox.Image = xrOk.Image;
            pictureBox.Padding = new DevExpress.XtraPrinting.PaddingInfo(10, 0, 0, 0);
            pictureBox.Sizing = DevExpress.XtraPrinting.ImageSizeMode.ZoomImage;
            pictureBox.SizeF = new System.Drawing.SizeF(24, 24);
            pictureBox.Borders = DevExpress.XtraPrinting.BorderSide.None;
            pictureBox.LocationF = new PointF(0, -10);

            //pictureBox.WidthF = report.PageWidth - report.Margins.Left - report.Margins.Right;
            //pictureBox.HeightF = 500;
            cell.Controls.Add(pictureBox);

            return cell;

        }

        private static XRTableCell AddCell(DataRow detail, XRTableRow xrow, string fieldName, bool leftAligned = false, int padding = 10, string postfix = "")
        {
            XRTableCell xcell = new XRTableCell();
            xcell.Text = detail[fieldName].ToStringNN() + postfix;
            if (leftAligned)
            {
                xcell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
                xcell.Padding = new DevExpress.XtraPrinting.PaddingInfo(padding, 0, 0, 0);
            }

            xrow.Cells.Add(xcell);
            return xcell;
        }

        private static XRTableCell AddCell(XRTableRow xrow, string fieldName, string val)
        {
            XRTableCell xcell = new XRTableCell();
            xcell.Text = val;
            xrow.Cells.Add(xcell);
            return xcell;
        }

        private static XRTableCell AddNumericCell(DataRow detail, XRTableRow xrow, string fieldName, string prefix = "")
        {
            string val = prefix + detail[fieldName].ToDecimalNN().ToString("n");
            return AddCell(xrow, fieldName, val);
        }

        private static XRTableCell AddDateCell(DataRow detail, XRTableRow xrow, string fieldName)
        {
            string val = "";
            if (detail[fieldName] != DBNull.Value)
                val = ((DateTime)detail[fieldName]).ToString("dd.MM.yyyy");
            return AddCell(xrow, fieldName, val);
        }


        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            //this.DataSource = ds;
            //this.DataMember = detailTableName;

            //BindCell(detailKalite, "Article");
            //BindCell(detailDesen, "Design");
            //BindCell(detailComposition, "Composition");
            //BindCell(detailWidth, "Width");
            //BindCell(detailWeight, "Weight");
            //if (lblMiktar.Text.Contains("kg"))
            //    BindCell(detailMt, "KG", "{0:n}");
            //else
            //    BindCell(detailMt, "Meters", "{0:n}");
            //BindCell(detailPrice, "Price", "{0:n}");
            //BindCell(detailTermin, "ETA", "{0:dd.MM.yyyy}");
            //BindCell(detailNotes, "Notes");
            //BindCell(detailMusteriKodu, "MusteriKodu");
        }

        private void BindCell(XRTableCell cell, string columnName, string format = "")
        {
            if (format == "")
                cell.DataBindings.Add("Text", null, detailTableName + "." + columnName);
            else
                cell.DataBindings.Add("Text", null, detailTableName + "." + columnName, format);
        }

        private void OrderReport_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            //{
            //    //trDetail.Cells.Remove(detailPhoto);

            //    for (int i = 0; i < tblHeader.Rows[0].Cells.Count; i++)
            //    {
            //        tblDetail.Rows[0].Cells[i].WidthF = tblHeader.Rows[0].Cells[i].WidthF;
            //    }

            //    trDetail.HeightF = 10;
            //    Detail.HeightF = 10;
            //}
        }

        private void tblDetail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            //if (detailNotes.Text == "")
            //{
            //    //    detailNotes.HeightF = 0;
            //    //    detailNotes.Visible = false;
            //    detailNotes.Borders = DevExpress.XtraPrinting.BorderSide.None;
            //    detailMusteriKodu.Visible = false;
            //    //pbArrow.Visible = false;
            //}
            //else
            //{
            //    detailNotes.Borders = ((DevExpress.XtraPrinting.BorderSide)(((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right)
            //| DevExpress.XtraPrinting.BorderSide.Bottom)));
            //    detailMusteriKodu.Visible = true;
            //    //pbArrow.Visible = true;
            //}
            ////    detailNotes.HeightF = detailNotes.HeightF;
        }

        private void trDetail2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (detailMusteriKodu.Text + detailNotes.Text == "")
                e.Cancel = true;

        }

        private void pnlOnay_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

        }

        private void pnlOnay_PrintOnPage(object sender, PrintOnPageEventArgs e)
        {
            if (e.PageIndex != e.PageCount - 1)
                e.Cancel = true;
        }

        private void pnlOnayFooter_PrintOnPage(object sender, PrintOnPageEventArgs e)
        {
            if (e.PageIndex == e.PageCount - 1)
                e.Cancel = true;
        }
    }
}